package com.wyz.controller;

/**
 * *@description
 * *@author wyz
 * *@date 2024/7/22
 **/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyz.mapper.ContactMapper;
import com.wyz.model.Contact;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description 联系人页面处理器
 */
@Controller
public class ContactController {
    @Resource
    private ContactMapper contactMapper;

    @RequestMapping("/contact/add.do")
    public String contactAdd(Contact contact,HttpSession session) {
        //从session中获取用户登录账号
        String account = (String)session.getAttribute("account");
        contact.setAccount(account);
        //数据库插入联系人
        contactMapper.insert(contact);
        return "redirect:/contact/list.do"; // 跳转到联系人页面
    }

    @RequestMapping("/contact/list.do")
    public String contactList(HttpSession session) {
        //从session中获取登录账号
        String account = (String) session.getAttribute("account");
        //查询联系人
        List<Contact> contactList = contactMapper.selectList(new QueryWrapper<Contact>().eq("account", account));
        //将联系人保存到session中
        session.setAttribute("contacts", contactList);
        return "contact";
    }

    @RequestMapping("/contact/del.do")
    public String contactDel(Integer id) {
        //数据库删除联系人
        contactMapper.deleteById(id);
        return "redirect:/contact/list.do"; // 重定向跳转到联系人页面
    }



    @RequestMapping("/contact/edit.do")
    public String contactEdit(Integer id,HttpSession session) {
        //从session中获取登录账号
        String account = (String)session.getAttribute("account");
        //查询修改的联系人信息
        Contact contact = contactMapper.selectById(id);
        //将修改的联系人信息存放到session中
        session.setAttribute("contact", contact);
        return "contactEdit";
    }



    @RequestMapping("/contact/editHandler.do")
    public String contactEditHandler(Contact contact) {
        //数据库修改联系人
        contactMapper.updateById(contact);
        return "redirect:/contact/list.do"; // 重定向跳转到联系人页面
    }
}